import java.util.ArrayList;


public class BufferedTcpSender implements Runnable{
	private String sendBufferToAll = ""; // Buffer for messages to all clients
	private ArrayList<String> sendBufferToSingle = new ArrayList<String>(); // Buffer for messages to single clients
	private ArrayList<Client> recipientBuffer = new ArrayList<Client>(); // Buffer parallel to the single recipient buffer, containing the recipients
	private Thread thread; 
	private boolean running = false;

	/**
	 * Constructs a BufferedTcpSender. This sender will continuously empty and send its buffer.
	 */
	public BufferedTcpSender(){

	}

//	/**
//	 * Adds a TCP message to the buffer, sending this string to all connected clients.
//	 * @param tcpMessage The TCP message to send to all connected clients. Newline 
//	 * character not required.
//	 */
//	public synchronized void addToBuffer(String tcpMessage){
//		synchronized (sendBufferToAll.intern()) {
//			if (sendBufferToAll != "") // Make sure the messages are split by newlines.
//				sendBufferToAll += "\n";
//			sendBufferToAll += tcpMessage;
//		}
//
//	}
//	
//	/**
//	 * Adds a TCP message to the buffer, sending it to the specified client only.
//	 * @param tcpMessage The TCP message to send to the specified client. Newline character 
//	 * not required.
//	 * @param client The client to send the TCP message to.
//	 */
//	public synchronized void addToBufferSingleRecipient(String tcpMessage, Client client){
//		synchronized (sendBufferToSingle) {
//			if (client != null)
//			{
//				sendBufferToSingle.add(tcpMessage);
//				recipientBuffer.add(client);
//			}
//			else System.out.println("Client is null in BufferedTcpSender.addToBufferSingleRecipient().");
//		}
//	}

	/**
	 * Start the buffered TCP sender.
	 */
	public void start(){
		ConnectionHandler.getInstance().getPinger().start();
		System.out.println("Pinger started.");
		running = true;
		thread = new Thread(this); 
		thread.start();
	}
	
	/**
	 * Stop the buffered TCP sender.
	 */
	public void stop(){
		ConnectionHandler.getInstance().getPinger().stop();
		this.running = false;
		this.thread = null;
	}

	@Override
	public void run(){
//		while(running){
//			long start = System.currentTimeMillis();
//			if(!this.sendBufferToAll.isEmpty()){
//				synchronized (this.sendBufferToAll.intern()) {
//
//					ArrayList<Client> clients = ConnectionHandler.getInstance().getActiveClients();
//					for(int i = 0; i < clients.size(); i++){
//						clients.get(i).sendToClient(this.sendBufferToAll);
//					}
//					this.sendBufferToAll = "";
//				}
//			}
//			if (!this.sendBufferToSingle.isEmpty())
//			{
//				synchronized (this.sendBufferToSingle) {
//					int recipientCount = this.sendBufferToSingle.size();
//					for (int i = 0; i < recipientCount; i++)
//					{
//						String data = this.sendBufferToSingle.get(i);
//						this.recipientBuffer.get(i).sendToClient(data);
//					}
//					this.sendBufferToSingle.clear();
//					this.recipientBuffer.clear();
//				}
//			}
//			try {
//				Thread.sleep(1);
//				long end = System.currentTimeMillis();
//				long elapsed = end - start;
//				if (elapsed > 5)
//					System.out.println("Time elapsed: " + elapsed);
//			} catch (InterruptedException e) {}	
//		}
	}
}
